package com.haojiang.mh.remote.adapter


import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.AreaValue
import com.haojiang.mh.remote.bean.ControlLeftMenuBean
import com.haojiang.mh.remote.enums.EMemory
import com.haojiang.mh.remote.enums.EMotor
import com.haojiang.mh.remote.enums.EMotorBtn
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.LogUtil
import com.haojiang.mh.remote.widget.*


class MemoryFragAdapter : RecyclerView.Adapter<MemoryFragAdapter.ViewHolder>() {

    private val viewList: MutableList<Int> = mutableListOf()
    private val dataMap: MutableMap<Int, MutableList<AreaValue>> = mutableMapOf()


    fun setData(memoryMap: MutableMap<Int, MutableList<AreaValue>>) {
            viewList.clear()
            dataMap.clear()
            for ((key, value) in memoryMap) {
                viewList.add(key)
//                LogUtil.e(value.toString() + "==记忆位置===")
                dataMap.put(key, value)
            }
            notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return viewList.size
    }


    /**
     * 设置界面
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemView = viewList.get(position)
        val cmdList = dataMap.get(itemView)
        if (cmdList != null) {
            BleUtil.traverseView(holder.itemView, cmdList)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return viewList.get(position)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        when (viewType) {
            EMemory.TV.key -> {
                return ViewHolder(
                    MemTvItemView(parent.context)
                )
            }

            EMemory.ZG.key -> {
                return ViewHolder(
                    MemZgItemView(parent.context)
                )
            }

            EMemory.SNORE.key -> {
                return ViewHolder(
                    MemSnoreItemView(parent.context)
                )
            }

            EMemory.LOUNGE.key -> {
                return ViewHolder(
                    MemLoungeItemView(parent.context)
                )
            }

            EMemory.M1.key -> {
                return ViewHolder(
                    MemM1ItemView(parent.context)
                )
            }

            EMemory.M2.key -> {
                return ViewHolder(
                    MemM2ItemView(parent.context)
                )
            }

            EMemory.M3.key -> {
                return ViewHolder(
                    MemM3ItemView(parent.context)
                )
            }

            EMemory.LUMBAR.key -> {
                return ViewHolder(
                    MemLumbarItemView(parent.context)
                )
            }

            EMemory.LUMBAR.key -> {
                return ViewHolder(
                        MemLumbarItemView(parent.context)
                )
            }

//            ITEM_FLAT -> {
//                return MemoryFragViewHolder(
//                    MemFlatItemView(parent.context)
//                )
//            }

            EMemory.FLAT2.key -> {
                return ViewHolder(
                    MemFlat2ItemView(parent.context)
                )
            }

            EMemory.FACTORY_RESET.key -> {
                return ViewHolder(
                    MemFacotryResetItemView(parent.context)
                )
            }

            EMemory.RELAX.key -> {
                return ViewHolder(
                    MemRelaxItemView(parent.context)
                )
            }

            EMemory.HEAD_UP_FOOT_UP.key -> {
                return ViewHolder(
                        MemHeadUpFootUpItemView(parent.context)
                )
            }
            EMemory.HEAD_UP_FOOT_DOWN.key -> {
                return ViewHolder(
                        MemHeadUpFootDownItemView(parent.context)
                )
            }
            EMemory.HEAD_DOWN_FOOT_UP.key -> {
                return ViewHolder(
                        MemHeadDownFootUpItemView(parent.context)
                )
            }
            EMemory.HEAD_UP_ONLY.key -> {
                return ViewHolder(
                        MemHeadUpOnlyItemView(parent.context)
                )
            }
            EMemory.FOOT_UP_ONLY.key -> {
                return ViewHolder(
                        MemFootUpOnlyItemView(parent.context)
                )
            }
        }
        return ViewHolder(
            MemTvItemView(parent.context)
        )
    }


    /**
     * adapter的ViewHolder类
     */
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}


    interface IMotortemClickListener {
        fun onItemClick(
            homeMenuItemBean: ControlLeftMenuBean
        )
    }
}
